package com.zygh.policeasset.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.AssetDisposalMapper;
import com.zygh.policeasset.mapper.AssetsMapper;
import com.zygh.policeasset.mapper.QrCodeInfoMapper;
import com.zygh.policeasset.pojo.*;
import com.zygh.policeasset.pojo.dto.QrCodeInfoDTO;
import com.zygh.policeasset.pojo.dto.UpdateBatchAssetsNumDTO;
import com.zygh.policeasset.service.AdjustAssetsCardService;
import com.zygh.policeasset.service.AssetsImagesService;
import com.zygh.policeasset.service.AssetsService;
import com.zygh.policeasset.service.DailyProjectService;
import com.zygh.policeasset.service.ProjectService;
import com.zygh.policeasset.service.SettlementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RequestMapping({"/assets"})
@Api(tags = {"资产管理模块(数据整合后)"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/AssetsController.class */
public class AssetsController {
    @Autowired
    AssetsService assetsService;
    @Autowired
    SettlementService settlementService;
    @Autowired
    DailyProjectService dailyProjectService;
    @Autowired
    ProjectService projectService;
    @Autowired
    AdjustAssetsCardService adjustAssetsCardService;
    @Autowired
    AssetsImagesService assetsImagesService;
    @Autowired
    AssetDisposalMapper assetDisposalMapper;
    @Autowired
    QrCodeInfoMapper qrCodeInfoMapper;
    @Autowired
    AssetsMapper assetsMapper;

    @RequestMapping(value = {"/findAll"}, method = {RequestMethod.GET})
    @ApiOperation("按条件数据查询")
    public Object findAll(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                          @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, Assets assets) {
        if (StringUtils.isEmpty(assets.getGetDatetime1()) && !StringUtils.isEmpty(assets.getGetDatetime2())) {
            return new CommonResult().validateFailed("请输入取得开始日期");
        }
        if (!StringUtils.isEmpty(assets.getGetDatetime1()) && StringUtils.isEmpty(assets.getGetDatetime2())) {
            return new CommonResult().validateFailed("请输入取得结束日期");
        }
        if (StringUtils.isEmpty(assets.getStartDatetime1()) && !StringUtils.isEmpty(assets.getStartDatetime2())) {
            return new CommonResult().validateFailed("请输入提折旧开始日期");
        }
        if (!StringUtils.isEmpty(assets.getGetDatetime1()) && StringUtils.isEmpty(assets.getGetDatetime2())) {
            return new CommonResult().validateFailed("请输入提折旧结束日期");
        }
        if (StringUtils.isEmpty(assets.getStartProvisionDate1()) && !StringUtils.isEmpty(assets.getStartProvisionDate2())) {
            return new CommonResult().validateFailed("请输入启用日期开始日期");
        }
        if (!StringUtils.isEmpty(assets.getStartProvisionDate1()) && StringUtils.isEmpty(assets.getStartProvisionDate2())) {
            return new CommonResult().validateFailed("请输入启用日期结束日期");
        }
        if (StringUtils.isEmpty(assets.getCompletionTime1()) && !StringUtils.isEmpty(assets.getCompletionTime2())) {
            return new CommonResult().validateFailed("请输入竣工验收日期开始日期");
        }
        if (!StringUtils.isEmpty(assets.getCompletionTime1()) && StringUtils.isEmpty(assets.getCompletionTime2())) {
            return new CommonResult().validateFailed("请输入启用日期结束日期");
        }
        if (StringUtils.isEmpty(assets.getCreateTime1()) && !StringUtils.isEmpty(assets.getCreateTime2())) {
            return new CommonResult().validateFailed("请输入录入时间开始日期");
        }
        if (!StringUtils.isEmpty(assets.getCreateTime1()) && StringUtils.isEmpty(assets.getCreateTime2())) {
            return new CommonResult().validateFailed("请输入录入时间结束日期");
        }

        if (StringUtils.isEmpty(assets.getQrCodeGetTime1()) && !StringUtils.isEmpty(assets.getQrCodeGetTime2())) {
            return new CommonResult().validateFailed("请输入生成二维码开始日期");
        }
        if (!StringUtils.isEmpty(assets.getQrCodeGetTime1()) && StringUtils.isEmpty(assets.getQrCodeGetTime2())) {
            return new CommonResult().validateFailed("请输入生成二维码结束日期");
        }

        if (!StringUtils.isEmpty(assets.getBigNum()) || StringUtils.isEmpty(assets.getSmallNum())) {
            return new CommonResult().success(this.assetsService.findAll(pageNum, pageSize, assets));
        }
        return new CommonResult().validateFailed("请选择资产大类");
    }

    @RequestMapping(value = {"/selectByQrCode"}, method = {RequestMethod.GET})
    @ApiOperation("根据二维码编号查询")
    public Object selectByQrCode(@RequestParam String qrCode) {
        return this.assetsService.selectByQrCode(qrCode);
    }

    @RequestMapping(value = {"/updateNum"}, method = {RequestMethod.POST})
    @ApiOperation("更新二维码序号")
    public Object updateNum(@RequestBody QrCodeInfoDTO qrCodeInfoDTO) {
        if (this.qrCodeInfoMapper.updateNum(qrCodeInfoDTO.getQrNum(), qrCodeInfoDTO.getQrCode()).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新失败");
    }

    @RequestMapping(value = {"/findAllExport"}, method = {RequestMethod.GET})
    @ApiOperation("按条件导出")
    public Object findAllExport(Assets assets, HttpServletResponse response) {
        return this.assetsService.findAllExport(response, assets);
    }

    @RequestMapping(value = {"/findAllNoPage"}, method = {RequestMethod.GET})
    @ApiOperation("按条件数据查询（不分页）")
    public Object findAllNoPage(Assets assets) {
        return this.assetsService.findAllNoPage(assets);
    }

    @RequestMapping(value = {"/assetsCardQuery"}, method = {RequestMethod.GET})
    @ApiOperation("资产卡片查询")
    public Object assetsCardQuery(@RequestParam String assetIds) {
        return this.assetsService.assetsCardQuery(assetIds);
    }

    @RequestMapping(value = {"/deleteByIds"}, method = {RequestMethod.POST})
    @ApiOperation("批量删除")
    public Object deleteByIds(@RequestBody JSONObject json) {
        if (this.assetsService.deleteByIds(json.getString("ids")).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/updateStartProvisionDate"}, method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "startProvisionDate", value = "入账日期,为空时传null", required = false, paramType = "query"), @ApiImplicitParam(name = "certificateNum", value = "入账凭证编号,为空时传null", required = false, paramType = "query"), @ApiImplicitParam(name = "buildNum", value = "项目编号", required = false, paramType = "query"), @ApiImplicitParam(name = "sectionId", value = "标段ID", required = false, paramType = "query"), @ApiImplicitParam(name = "type", value = "jc或zn或cg", required = false, paramType = "query")})
    @ApiOperation("修改开始计提折旧日期和入账凭证号")
    public Object updateStartProvisionDate(@RequestBody List<StartProvisionDate> list) {
        try {
            for (StartProvisionDate startProvisionDate : list) {
                Assets assets = new Assets();
                assets.setBuildNum(startProvisionDate.getBuildNum());
                assets.setSectionId(startProvisionDate.getSectionId());
                if (this.assetsService.findAllNoPage(assets).size() <= 0) {
                    return new CommonResult().validateFailed("当前项目无资产，请录入资产");
                }
                if ("jc".equals(startProvisionDate.getType()) || "zn".equals(startProvisionDate.getType())) {
                    if (Integer.valueOf(this.settlementService.updateSPD(startProvisionDate.getStartProvisionDate(), startProvisionDate.getCertificateNum(), startProvisionDate.getBuildNum(), startProvisionDate.getSectionId())).intValue() == 0) {
                        return new CommonResult().validateFailed("修改审计结算失败");
                    }
                } else if ("da".equals(startProvisionDate.getType())) {
                    if (this.dailyProjectService.updateSPD(startProvisionDate.getStartProvisionDate(), startProvisionDate.getCertificateNum(), Integer.valueOf(Integer.parseInt(startProvisionDate.getBuildNum()))) == 0) {
                        return new CommonResult().validateFailed("修改项目信息失败");
                    }
                } else if ("cg".equals(startProvisionDate.getType()) && this.projectService.updateSPD(startProvisionDate.getBuildNum(), startProvisionDate.getStartProvisionDate(), startProvisionDate.getCertificateNum()).intValue() == 0) {
                    return new CommonResult().validateFailed("修改项目信息失败");
                }
                this.assetsService.updateStartProvisionDate(startProvisionDate.getStartProvisionDate(), startProvisionDate.getCertificateNum(), startProvisionDate.getBuildNum(), startProvisionDate.getSectionId()).intValue();
            }
            return new CommonResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/updateById"}, method = {RequestMethod.POST})
    @ApiOperation("通过ID修改")
    public Object updateById(@RequestBody Assets assets) {
        if (this.assetsService.updateById(assets).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("资产修改失败");
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("资产添加")
    public Object add(@RequestBody Object t) {
        if (this.assetsService.add(t).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("资产添加失败");
    }

    @RequestMapping(value = {"/updassetsClassificationId"}, method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "资产主键集合，逗号隔开", required = true, paramType = "query"), @ApiImplicitParam(name = "assetsClassificationId", value = "资产新分类Id,为空时传null", required = true, paramType = "query"), @ApiImplicitParam(name = "assetsClassificationPid", value = "资产新分类父Id,为空时传null", required = true, paramType = "query")})
    @ApiOperation("修改资产分类编号")
    public Object updassetsClassificationId(@RequestBody JSONObject json) {
        if (this.assetsService.updAssetsClassificationId(json.getString("ids"), json.getInteger("assetsClassificationId"), json.getInteger("assetsClassificationPid")).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败");
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("根据ID查询")
    public Object findById(Integer id) {
        return this.assetsService.findById(id);
    }

    @RequestMapping(value = {"/findByIds"}, method = {RequestMethod.GET})
    @ApiOperation("根据ID查批量询")
    public Object findByIds(@RequestParam String ids) {
        return this.assetsService.findByIds(ids.split(","));
    }

    @RequestMapping(value = {"/findByProject"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "buildNum", value = "项目编号", required = true, paramType = "query"), @ApiImplicitParam(name = "sectionId", value = "标段ID", required = false, paramType = "query"), @ApiImplicitParam(name = "name", value = "资产名称", required = true, paramType = "query"), @ApiImplicitParam(name = "assetsClassificationId", value = "是否设置资产大类小类，0是未设置，1是已设置", required = true, paramType = "query"), @ApiImplicitParam(name = "assetsType", value = "jc、zns、zns、da、cg", required = true, paramType = "query")})
    @ApiOperation("在项目中查询资产信息")
    public Object findByProject(@RequestParam(required = false) String buildNum, @RequestParam(required = false) Integer sectionId, @RequestParam(required = false) String name, @RequestParam(required = false) Integer assetsClassificationId, @RequestParam(required = false) String assetsType) {
        return this.assetsService.findByProject(buildNum, sectionId, name, assetsClassificationId, assetsType);
    }

    @RequestMapping(value = {"/depRateStatistics"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "startTime", value = "启用起始日期", required = true, paramType = "query"), @ApiImplicitParam(name = "endTime", value = "启用结束日期", required = true, paramType = "query"), @ApiImplicitParam(name = "time1", value = "折旧起始日期", required = true, paramType = "query"), @ApiImplicitParam(name = "time2", value = "折旧结束日期", required = true, paramType = "query"), @ApiImplicitParam(name = "condition", value = "是否过滤已折旧完数据(0是移除、1是不移除)", required = true, paramType = "query"), @ApiImplicitParam(name = "dataLabelsId", value = "数据标签", required = true, paramType = "query"), @ApiImplicitParam(name = "assetsClassificationId", value = "资产子ID", required = true, paramType = "query"), @ApiImplicitParam(name = "assetsClassificationPid", value = "资产父ID", required = true, paramType = "query")})
    @ApiOperation("查询时间段的的资产在时间段内的折旧(新)")
    public Object depratebytimeNew(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String time1, @RequestParam(required = false) String time2, @RequestParam(required = false) Integer condition, @RequestParam(required = false) Integer dataLabelsId, @RequestParam(required = false) Integer assetsClassificationId, @RequestParam(required = false) Integer assetsClassificationPid) throws Exception {
        return this.assetsService.depRateStatistics(startTime, endTime, time1, time2, condition, dataLabelsId, assetsClassificationId, assetsClassificationPid);
    }

    @RequestMapping(value = {"/assetsSubmit"}, method = {RequestMethod.POST})
    @ApiOperation("资产信息提交")
    public Object assetsSubmit(@RequestBody NewApprovalProcess newApprovalProcess) {
        try {
            if (this.assetsService.checkSubmitted(newApprovalProcess).intValue() > 0) {
                return new CommonResult().validateFailed("资产已提交");
            }
            if (this.assetsService.checkNo(newApprovalProcess).intValue() == 0) {
                return new CommonResult().validateFailed("资产未录入，请先录入资产");
            }
            this.assetsService.assetsSubmit(newApprovalProcess);
            return new CommonResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/approve"}, method = {RequestMethod.POST})
    @ApiOperation("资产审批")
    public Object approve(@RequestBody NewApprovalProcess newApprovalProcess) {
        try {
            this.assetsService.approve(newApprovalProcess);
            return new CommonResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/errorCorrection"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("资产卡片纠错")
    public Object errorCorrection(@RequestBody JSONObject json) {
        Assets assets = new ObjectMapper().convertValue(json.get("list"), Assets.class);
        String reason = json.getString("reason");
        Integer userId = json.getInteger("userId");
        try {
            this.assetsService.updateById(assets);
            this.adjustAssetsCardService.add(reason, userId, 1, assets.getId().toString());
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/invalid"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("资产卡片批量作废")
    public Object invalid(@RequestBody JSONObject json) {
        try {
            for (String id : json.getString("ids").split(",")) {
                if (this.assetsMapper.getAssetsStatus(Integer.valueOf(Integer.parseInt(id))).intValue() == -1) {
                    return new CommonResult().validateFailed("选择的资产中有已作废的资产,请核对");
                }
            }
            this.assetsService.updById(json.getString("ids"), json.getInteger("userId"));
            this.adjustAssetsCardService.add(json.getString("reason"), json.getInteger("userId"), 2, json.getString("ids"));
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/breakUp"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("资产卡片拆分")
    public Object breakUp(@RequestBody JSONObject json) {
        String reason = json.getString("reason");
        Integer userId = json.getInteger("userId");
        Integer assetsId = json.getInteger("assetsId");
        List<Assets> assetsList = changeList(json.get("list"), Assets.class);
        if (assetsList.size() == 1) {
            return new CommonResult().validateFailed("资产信息未拆分，请核对");
        }
        try {
            this.assetsService.breakUp(assetsList, reason, userId, assetsId);
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public static <T> List<T> changeList(Object object, Class<T> clazz) {
        try {
            ArrayList arrayList = new ArrayList();
            if (!(object instanceof List)) {
                return null;
            }
            for (Object o : (List) object) {
                arrayList.add(JSONObject.parseObject(JSONObject.toJSONString(o), clazz));
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = {"/assetsDisposal"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("资产处置")
    public Object assetsDisposal(@RequestBody JSONObject json) {
        try {
            this.assetsService.updAssetsDisposal(json.getString("ids"), json.getInteger("userId"), json.getInteger("type"));
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/dockingAssets"}, method = {RequestMethod.GET})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("根据资产编号查询工单")
    public Object assetsDisposal(@RequestParam String assetsNum) {
        try {
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("assetsNum", assetsNum);
            return new CommonResult().success(HttpUtil.get("http://41.6.251.11:12402/dockingAssets/list", paramMap));
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/selectCombined"}, method = {RequestMethod.GET})
    @ApiOperation("根据项目编号和标段编号查询总金额")
    public Object selectCombined(@RequestParam(value = "whetherSet", required = false) Integer whetherSet, @RequestParam(value = "assetsType", required = false) String assetsType, @RequestParam(value = "buildNum", required = false) String buildNum, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "sectionId", required = false) Integer sectionId, @RequestParam(value = "assetsStatus", required = false) Integer assetsStatus) {
        try {
            return new CommonResult().success(this.assetsService.selectCombined(whetherSet, assetsType, buildNum, name, sectionId, assetsStatus));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/addLight"}, method = {RequestMethod.POST})
    @ApiOperation("信号灯资产添加")
    public Object addLight(@RequestBody AddLightDto addLightDto) {
        if (this.assetsService.addLight(addLightDto).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加失败");
    }

    @RequestMapping(value = {"/deleteLight"}, method = {RequestMethod.POST})
    @ApiOperation("信号灯资产删除")
    public Object deleteLight(@RequestBody AddLightDto addLightDto) {
        if (this.assetsService.deleteLight(addLightDto).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/updateBatchAssetsNum"}, method = {RequestMethod.POST})
    @ApiOperation("批量修改资产编号")
    public Object updateBatchAssetsNum(@RequestBody UpdateBatchAssetsNumDTO updateBatchAssetsNumDto) {
        if (this.assetsService.updateBatchAssetsNum(updateBatchAssetsNumDto).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/beyondThe"}, method = {RequestMethod.GET})
    @ApiOperation("查询折旧到期资产")
    public Object beyondThe(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize, Assets assets) {
        return this.assetsService.beyondThe(pageNum, pageSize, assets);
    }

    @RequestMapping(value = {"/pushBeyondTheAssets"}, method = {RequestMethod.GET})
    @ApiOperation("推送折旧到期资产")
    public Object pushBeyondTheAssets() {
        return this.assetsService.pushBeyondTheAssets();
    }


    @PostMapping(value = {"/generateCode"})
    @ApiOperation("批量生成资产id")
    public Object generateCode(@RequestBody JSONObject json) {
        return assetsService.generateCode(json.getString("ids"));
    }

    @PostMapping(value = {"/bindingCards"})
    @ApiOperation("资产绑牌")
    public Object bindingCards(@RequestBody BindingCards bindingCards) {
        return assetsService.bindingCards(bindingCards);
    }
}